
libname ra "E:\data";/*cohort*/
libname pd "Z:\Medicare_20\Part_D";
libname pb "Z:\Medicare_20\Part_b";
libname mbsf "Z:\Medicare_20\mbsf";/*input dataset*/

***********PURPOSE: STACK ALL RX DATASETS(WITH DESC VARIABLES)  TO CREATE ANALYTICAL DATASET***************************
                  : GET MBSF VARS FOR LOW INCOME SUBSIDY IN THE YR PRIOR TO INDEX
                  : GET CONCOMITANT MEDICATION USE AND RHEUM VISIT IN THE YR PRIOR TO INDEX*****; 

****************************************************************************
**************************stack all rx datasets*****************************
****************************************************************************;

data ra.stacka;
length desc $40;
set ra.ada_5a ra.cert_5a ra.etan_9a ra.goli_d_5a
    ra.goli_5a ra.infl_5a;

index_desc= desc_dt-index_dt;

/*create index2 date, time zero for cox models*/

if index_med1 in ('Adalimumab', 'Certolizumab', 'Etanercept', 'Goli_d') then 
index2= intnx ('DAY',index_dt,183);/*6mths for part D index med*/

else if index_med1 in ('Goli_b', 'Inflixima') then
index2 = intnx ('DAY',index_dt,213);/*7mths for part b index med*/;
format index2 date9.;

run;/*n=4306*/


proc sort data=ra.mbsf_08_17_1 out=mbsf_08_17_1 ;by bene_id; run;

proc sort data= ra.stacka out=stacka; by bene_id;run;


data stack_1a;
  merge stacka (in=a) mbsf_08_17_1;
  by bene_id;

  if a;
  
*****from and thru position yr before index************;

	fromPosition_index2= ((year(index2) - 2008) * 12) + month(index2) - 11; /*position of one year before index2 dt*/
    thruPosition_index2 = ((year(index2) - 2008) * 12) + month(index2); /*position at index2 dt*/

   if fromPosition_index2 ge 1 and thruPosition_index2 le 120 then do;
    maxMonths_index2 = thruPosition_index2 - fromPosition_index2 + 1;
    
		   lis_pre= 0;
		   dual_pre=0;

            do lcv = fromPosition_index2 to thruPosition_index2;

             if  substr(lis0817,lcv,1) = "1"  then lis_pre=  lis_pre +1;
           if  substr(dual0817,lcv,1) = "1"  then dual_pre=  dual_pre +1;

            end;

   end;
 /*at any point during 12 month prior to index date maxMonths 12*/
   if lis_pre gt 0 and lis_pre le 12 then lis_pre_bin=1 ; else lis_pre_bin=0;
   if dual_pre gt 0 and dual_pre le 12 then dual_pre_bin=1 ; else dual_pre_bin=0;


 drop dual0817 lis0817 fromPosition_index2 thruPosition_index2 maxMonths_index2 lcv
diff_int_w1 diff_int_w2 next1_diff_int_w next2_diff_int_w
event_cess_first event_both_first eventboth_dt_first event_incint_first event_dosered_first
dose_red_dt cess_dt_first incintdt_first next1_diff_dose_int next2_diff_dose_int lag_days_suply
sum_diff_int sum_rx_int diff_dose_int2 lag_int6 lag_rx_incint6 diff_dose_int1 int_6
rx_incint6 lag_dose_int_w lag_other_bio lag_prim_med index_rx  lag_rx_file lag_dose_d 
CLM_ID CLM_FROM_DT CLM_THRU_DT LINE_NCH_PMT_AMT CLM_PMT_AMT RA_B_ICD_bef12 RA_B_ICD_aft6 bef12m_ra;/*checked*/
 

 run;


 ********************CONCOMITANT RX USE**************************************************************
 ****************************************************************************************************

******create index med1 to distinguish index b and index d meds in the prev version of dat***********;

 
proc sort data= ra.coh_ana1; by bene_id;run;
data med1;
set ra.coh_ana1;
length index_med1 $20;
by bene_id;
if first.bene_id;

  if index_med= "Golimumab" and rx_file = "b" then index_med1= "Goli_b";
  else if index_med= "Golimumab" and rx_file = "d" then index_med1= "Goli_d";
  else index_med1= index_med;

/*create index2 date, time zero for cox models*/
   if index_med1 in ('Adalimumab', 'Certolizumab', 'Etanercept', 'Goli_d') then 
  index2= intnx ('DAY',index_dt,183);/*6mths for part D index med*/
  else if index_med1 in ('Goli_b', 'Inflixima') then
  index2 = intnx ('DAY',index_dt,213);/*7mths for part b index med*/;

  format index2 date9.;


keep bene_id index_med1 index2;
run;

proc sort data= med1; by bene_id;run;

data coh_ana1;
merge ra.coh_ana1 med1;
by bene_id;
run;


************get rows with csdamrds, GC amd methotrex medication use 6m prior to index2**************;
data cs_gc_6m;
  set coh_ana1;

  index2_rx_int=  rx_dt - index2;/*time between index and prescrip fill*/

  where med_group in ("GC", "csDMARD") ;/*keep rwos with GC or CSDMARD*/

  if index2_rx_int ge 0 and index2_rx_int le 183;/*keep rows within 6 months of index2 for table1*/

keep bene_id index_dt index2 index_med rx_dt medication_name med_group index2_rx_int;
run;


****************create seperate datasets for each med*****************;
data gc;
    set cs_gc_6m;
if med_group= "GC";
run;

proc sort data= gc;by bene_id;run;

data gc_6m_1rps;
  set gc;
  by bene_id;
  if first.bene_id;
    GC_6m_index2=1;/*var to indicate gc use 6m prior to index*/
keep bene_id GC_6m_index2;
run;

***cs***;
data cs;
    set cs_gc_6m;
   if med_group= "csDMARD";
run;

proc sort data= cs; by bene_id;run;

data cs_6m_1rps;
    set cs;
by bene_id;

if first.bene_id;
CS_6m_index2=1;
keep bene_id CS_6m_index2;
run;

****methotrex****;
data mtx;
set cs_gc_6m;
if Medication_name= "Methotrexate";
run;

proc sort data= mtx;by bene_id;run;
data mtx_6m_1rps;
set mtx;
by bene_id;
if first.bene_id;
MTX_6m_index2=1;
keep bene_id MTX_6m_index2;
run;

   proc sort data= stack_1a ;by bene_id;run;
   proc sort data= gc_6m_1rps ;by bene_id;run;
   proc sort data= cs_6m_1rps ;by bene_id;run;
   proc sort data= mtx_6m_1rps ;by bene_id;run;


data stack_1a;
merge stack_1a (in=a) gc_6m_1rps cs_6m_1rps mtx_6m_1rps;
by bene_id;
if a;
run;

******************************************************************
**********************1 yr prior********************************;
***********************************************************************
************get rows with csdamrds, GC amd methotrexta 1 yr prior to index2************;
data cs_gc_1yr;
set coh_ana1;

 index2_rx_int=  rx_dt - index2;

  where med_group in ("GC", "csDMARD") ;/*keep rwos with GC or CSDMARD*/

/*keep rows within 1 yr prior to index2 for table1*/
  if index2_rx_int ge -365 and index2_rx_int le 0 and index2_rx_int ne . ;

keep bene_id index_dt index2 index_med rx_dt medication_name med_group index2_rx_int;
run;


****************create seperate datasets for each*****************;
data gc;
set cs_gc_1yr;
if med_group= "GC";
run;

proc sort data= gc;by bene_id;run;

data gc_1yr_1rps;
set gc;
by bene_id;
if first.bene_id;
GC_1yr_pr_index2=1;
keep bene_id GC_1yr_pr_index2;
run;

***cs***;
data cs;
set cs_gc_1yr;
if med_group= "csDMARD";
run;
proc sort data= cs; by bene_id;run;
data cs_1yr_1rps;
set cs;
by bene_id;
if first.bene_id;
CS_1yr_pr_index2=1;
keep bene_id CS_1yr_pr_index2;
run;

****methotrex****;
data mtx;
set cs_gc_1yr;
if Medication_name= "Methotrexate";
run;

proc sort data= mtx;by bene_id;run;
data mtx_1yr_1rps;
set mtx;
by bene_id;
if first.bene_id;
MTX_1yr_index2=1;
keep bene_id MTX_1yr_index2;
run;

proc sort data= stack_1a ;by bene_id;run;
proc sort data= gc_1yr_1rps ;by bene_id;run;
proc sort data= cs_1yr_1rps ;by bene_id;run;
proc sort data= mtx_1yr_1rps ;by bene_id;run;


data stack_1a;
merge stack_1a (in=a) gc_1yr_1rps cs_1yr_1rps mtx_1yr_1rps;
  by bene_id;
  if a;
run;


***************long term GC use (>90d) ******************************************;

data gc_6m;
    set coh_ana1;
    index2_rx_int=  rx_dt - index2;
where med_group in ("GC") ;/*keep rwos with GC or CSDMARD*/
if index2_rx_int ge 0 and index2_rx_int le 183;/*keep rows within 6 months of index2*/
keep bene_id index_dt rx_dt medication_name med_group index2_rx_int DAYS_SUPLY_NUM;
run;

proc means data= gc_6m sum noprint;
    by bene_id;
    var days_suply_num;
    output out=sum sum= sum_gc_6m;
run;

data sum;
set sum;
drop _TYPE_ _FREQ_;

if sum_gc_6m ge 90 then gc_long_6m=1; else gc_long_6m=0;
label sum_gc_6m = "sum of GC days supply index-6m";

run;
proc sort data= sum ;by bene_id;run;
proc sort data= stack_1a ; by bene_id;run;

data stack_1a;
merge stack_1a(in=a) sum;
by bene_id;
if a;
run;


*********************1yr prior to index2**************************;

data gc_1yr;
set coh_ana1;

   index2_rx_int=  rx_dt - index2;

  where med_group in ("GC") ;/*keep rwos with GC or CSDMARD*/

if index2_rx_int ge -365 and index2_rx_int le 0 and index2_rx_int ne . ;/*keep rows 1yr prior to index2*/

keep bene_id index_dt rx_dt medication_name med_group index2_rx_int DAYS_SUPLY_NUM;
run;

proc means data= gc_1yr sum noprint;
 by bene_id;
  var days_suply_num;
output out=sum sum= sum_gc_1yr;
run;

data sum;
    set sum;
    drop _TYPE_ _FREQ_;

   if sum_gc_1yr ge 90 then gc_long_1yr =1; else gc_long_1yr=0;
   label sum_gc_1yr = "sum of GC days suppl ir prior to index2";

run;

proc sort data= sum ;by bene_id;run;
proc sort data= stack_1a ; by bene_id;run;

data stack_1a;
  merge stack_1a(in=a) sum;
  by bene_id;
  if a;
run;


**************************************************************************
**********************RHEUMATOLOGIST VISIT*******************************
**************************************************************************;
************rhuem visit within 6 mo of index****;

proc sort data= ra.rarh_0817 out= rarh_0817; by bene_id ;run;
proc sort data= stack_1a out = stack_1b (keep= bene_id rx_dt rx_dt ra_dt index_dt desc index2); by bene_id ;run;

data stack_1b;
set stack_1b;
by bene_id;
if first.bene_id;
run;
proc sort data= stack_1b;by bene_id;
run;

********************dataset with multiple rheum visit per id (for future purposes)*****************;

***dataset with rows of 12m prior to index2 to index dt2 ***;
data rh_stack1;
merge stack_1b (in=a) rarh_0817;
by bene_id ;
if a;

rh_index2_int= rh_dt - index2;

if rh_index2_int ne . then do ;/*if rh visit*/
 if rh_index2_int ge -365 and rh_index2_int le 0 then keep=1;
 else  if rh_index2_int  lt -365 then keep=0;
 else if rh_index2_int gt 0 then keep=0;
 
 end;
  else if rh_index2_int = . then keep=1;/*no rh visit*/
  
run;

data rh_stack2;
set rh_stack1;
if keep=1 /* and (rh_index2_int= . or ( rh_index2_int ge 0 and rh_index2_int le 365))*/;
run;


proc sort data= rh_stack2 out=yy nodupkey;by bene_id;run;


proc sort data= rh_stack2; by bene_id;run;

data rh_stack2;
retain rh_visit;
set rh_stack2;

by bene_id;

if spclty ne " " then do;

 if first.bene_id then rh_visit=1;
  else rh_visit= 1+ rh_visit;

end;
if spclty = " " then rh_visit=. ;
run;
 

***************dataset with atleast 1 rh visit in 1 yr prior to index2 and merge with stack chohrt*************
******************use this dataset for tables and analyses**********************************;
data rh_visit;

   set rh_stack2;
    if rh_visit=1;
   rh_visit_1yr=1;
keep bene_id rh_visit_1yr;
run;

proc sort data= rh_visit; by bene_id;run;

proc sort data= stack_1a ; by bene_id;run;


data ra.stack_1a;
merge stack_1a rh_visit;

by bene_id;
  if rh_visit_1yr = . then rh_visit_1yr=0;

***************************AGE AT TIME ZERO***********************;
/*age at the time of origin (time 0)*/

age_index2= (index2- bene_birth_dt)/365;

***************combine race categories other, unknown, asian, native american******;

/*RTI RACE CODE             0-Unknown
			                1-Non-Hispanic White
			                2- Black
			                3-Other
			                4-Asian/Pacific ISlander
			                5-Hispanic
			                6- American Indian/Alaska NAtive*/


if RTI_RACE_CD in (0, 3, 4, 6)then race = 4;
else if RTI_RACE_CD = 5 then race= 3;
else race= RTI_RACE_CD;
label race= "race 1=NH white 2=Black 3=Hispanic 4=other" ;
run;

 








